#!/bin/bash

# conf1_xxx() handle conf format:
# key=value
#
# conf2_xxx() handle conf format:
# value1 | value2 | value3

function conf1_get()
{
	if [[ $# -lt 2 ]]; then
		echo "[${FUNCNAME}] Parameter error." >&2
		return 1
	fi
	local CONF_FILE=$1
	local KEY=$2

	if [[ ! -f ${CONF_FILE} ]]; then
		echo "[${FUNCNAME}] No such file: ${CONF_FILE}" >&2
		return 2
	fi

	local SET_STR=$(cat ${CONF_FILE} | grep -E "^${KEY}=")
	if [[ -n ${SET_STR} ]]; then
		eval ${SET_STR}
		echo ${!KEY}
	fi
}

function conf1_set()
{
	if [[ $# -lt 3 ]]; then
		echo "[${FUNCNAME}] Parameter error." >&2
		return 1
	fi
	local CONF_FILE=$1
	local KEY=$2
	local VAL=$3

	if [[ ! -f ${CONF_FILE} ]]; then
		touch ${CONF_FILE}
	fi

	local SET_STR=$(cat ${CONF_FILE} | grep -E "^${KEY}=")
	if [[ -n ${SET_STR} ]]; then
		sed -i "s/${SET_STR}/${KEY}=${VAL}/g" ${CONF_FILE}
	else
		echo "${KEY}=${VAL}" >> ${CONF_FILE}
	fi
}

function conf1_list()
{
	if [[ $# -lt 1 ]]; then
		echo "[${FUNCNAME}] Parameter error." >&2
		return 1
	fi
	local CONF_FILE=$1
	local READ_LINE
	local READ_KEY
	local READ_VAL

	if [[ ! -f ${CONF_FILE} ]]; then
		echo "[${FUNCNAME}] No such file: ${CONF_FILE}" >&2
		return 2
	fi

	cat ${CONF_FILE} | while read READ_LINE
	do
		READ_KEY=$(echo ${READ_LINE} | awk -F '=' '{print $1}')
		READ_VAL=$(echo ${READ_LINE} | awk -F '=' '{print $2}')
		echo -e "${READ_KEY}\t: ${READ_VAL}"
	done
}

function conf2_find()
{
	if [[ $# -lt 2 ]]; then
		echo "[${FUNCNAME}] Parameter error." >&2
		return 1
	fi
	local CONF_FILE=$1
	local KEY=$2

	local READ_LINE
	local COLUMN1
	local COLUMN2
	local COLUMN2_ARRAY
	local VAL

	if [[ ! -f ${CONF_FILE} ]]; then
		echo "[${FUNCNAME}] No such file: ${CONF_FILE}" >&2
		return 2
	fi

	cat ${CONF_FILE} | while read READ_LINE;
	do
		if [[ -z ${READ_LINE} || ${READ_LINE} =~ ^# ]]; then
			continue
		fi

		COLUMN1=$(echo ${READ_LINE} | awk -F '|' '{print $1}' | awk '{$1=$1; print}')
		COLUMN2=$(echo ${READ_LINE} | awk -F '|' '{print $2}' | awk '{$1=$1; print}')
		COLUMN2_ARRAY=(${COLUMN2//,/ })
		if [[ ${KEY} == ${COLUMN1} ]]; then
			echo ${COLUMN1}
		else
			for VAL in  ${COLUMN2_ARRAY[@]}; do
				if [[ ${KEY} == ${VAL} ]]; then
					echo ${COLUMN1}
				fi
			done
		fi
	done
}

function conf2_get_value()
{
	if [[ $# -lt 2 ]]; then
		echo "[${FUNCNAME}] Parameter error." >&2
		return 1
	fi
	local CONF_FILE=$1
	local KEY=$2
	local COLUMN_NUM=$3

	local READ_LINE
	local COLUMN1
	local COLUMN2
	local COLUMN3

	if [[ ! -f ${CONF_FILE} ]]; then
		echo "[${FUNCNAME}] No such file: ${CONF_FILE}" >&2
		return 2
	fi

	cat ${CONF_FILE} | while read READ_LINE;
	do
		if [[ -z ${READ_LINE} || ${READ_LINE} =~ ^# ]]; then
			continue
		fi

		COLUMN1=$(echo ${READ_LINE} | awk -F '|' '{print $1}' | awk '{$1=$1; print}')
		COLUMN2=$(echo ${READ_LINE} | awk -F '|' '{print $2}' | awk '{$1=$1; print}')
		COLUMN3=$(echo ${READ_LINE} | awk -F '|' '{print $3}' | awk '{$1=$1; print}')
		if [[ -n ${COLUMN1} && ${KEY} == ${COLUMN1} ]]; then
			if [[ ${COLUMN_NUM} == "2" ]]; then
				echo ${COLUMN2}
			elif [[ ${COLUMN_NUM} == "3" ]]; then
				echo ${COLUMN3}
			fi
		fi
	done
}

function conf2_list()
{
	if [[ $# -lt 2 ]]; then
		echo "[${FUNCNAME}] Parameter error." >&2
		return 1
	fi
	local CONF_FILE=$1
	local COLUMN_NUM_ARRAY=(${2//,/ })
	local COLUMN_NUM
	local COLUMN
	local READ_LINE

	if [[ ! -f ${CONF_FILE} ]]; then
		echo "[${FUNCNAME}] No such file: ${CONF_FILE}" >&2
		return 2
	fi

	cat ${CONF_FILE} | while read READ_LINE;
	do
		if [[ -z ${READ_LINE} || ${READ_LINE} =~ ^# ]]; then
			continue
		fi

		COLUMN=""
		for COLUMN_NUM in "${COLUMN_NUM_ARRAY[@]}"; do
			COLUMN+="\t"
			COLUMN+=$(echo ${READ_LINE} | awk -F '|' "{print \$${COLUMN_NUM}}" | awk '{$1=$1; print}')
		done
		echo -e ${COLUMN}
	done
}

########################################################
BSP_VER_CONF=${CUR_TOP}/conf/jetson_versions.txt

function conf2_find_jetpack_ver()
{
	conf2_find ${BSP_VER_CONF} $1
}

function conf2_get_l4t_ver()
{
	conf2_get_value ${BSP_VER_CONF} $1 2
}

function conf2_list_vers()
{
	echo -e "JetPack, L4T Versions:"
	conf2_list ${BSP_VER_CONF} 1,2
}

########################################################
MODULE_CONF=${CUR_TOP}/conf/jetson_modules.txt

function conf2_find_module_pnum()
{
	conf2_find ${MODULE_CONF} $1
}

function conf2_get_module_name_list()
{
	conf2_get_value ${MODULE_CONF} $1 2
}

function conf2_list_modules()
{
	echo -e "Jetson Modules:"
	conf2_list ${MODULE_CONF} 1,2
}

########################################################
BOARD_CONF=${CUR_TOP}/conf/jetson_boards.txt

function conf2_get_module_pnum_list_with_board()
{
	conf2_get_value ${BOARD_CONF} $1 2
}

function conf2_find_board_pnum_list_with_module()
{
	conf2_find ${BOARD_CONF} $1
}

function conf2_get_board_desc()
{
	conf2_get_value ${BOARD_CONF} $1 3
}

function conf2_list_boards()
{
	echo -e "Jetson Boards:"
	conf2_list ${BOARD_CONF} 1,3
}
